package com.woniu.humanresource.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniu.humanresource.entity.*;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.ActivityService;
import com.woniu.humanresource.service.EmployeesService;
import com.woniu.humanresource.service.InterviewerService;
import com.woniu.humanresource.utils.Base64Util;
import com.woniu.humanresource.utils.RealNameAuthentication;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class InterviewerController {
    @Value("${upload.addResourceLocation}")
    private String addResourceLocation;
    @Resource
    EmployeesService employeesService;
    @Resource
    InterviewerService interviewerService;
    @Resource
    ActivityService activityService;
    //分页应聘者信息
    @PostMapping("/interviewer/{pageIndex}/{pageSize}")
    public ResponseText<PageBean<Interviewer>> getInterviewers(@PathVariable("pageIndex") int page,
                                                         @PathVariable("pageSize") int pageSize,
                                                         @RequestBody Interviewer interviewer){
        ResponseText<PageBean<Interviewer>> responseText=null;
        try {

            PageBean<Interviewer> pageBean = interviewerService.selectCondition(interviewer, page, pageSize);
            responseText=new ResponseText<PageBean<Interviewer>>(pageBean,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //添加应聘者信息
    @PostMapping("/interviewer")
    public ResponseText addInterviewer(@RequestBody Interviewer interviewer){
        ResponseText responseText=null;
        try {
//            判断应聘表中有无此员工
            Interviewer interviewer1 = new Interviewer();
            interviewer1.setTelephone(interviewer.getTelephone());
            PageBean<Interviewer> interviewerPageBean = interviewerService.selectCondition(interviewer1, 1, 10);
//          判断在职表是否有此员工
            Employees employees = new Employees();
            employees.setTelephone(interviewer.getTelephone());
            PageBean<Employees> employeesPageBean = employeesService.selectEmpInfoCondition(employees, 1, 10);
//            没有此员工则添加
            if(interviewerPageBean.getBeanList().isEmpty()&&employeesPageBean.getBeanList().isEmpty()){
                interviewerService.insertInterviewer(interviewer);
//            定时修改状态
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        interviewer.setState(2);
                        interviewerService.updateInterviewer(interviewer);
                    }
                }, 86400000);
                responseText=new ResponseText(null,200,"success");
                return  responseText;
            }else {
//                有此员工需判断状态
                if(employeesPageBean.getBeanList().isEmpty()){
                    Interviewer interviewer2 = interviewerPageBean.getBeanList().get(0);
//                人才库状态或者离职状态可以重新录入
                    if(interviewer2.getState()==3||interviewer2.getState()==6){
                        interviewer2.setState(1);
                        interviewerService.updateInterviewer(interviewer2);
                        return new ResponseText(null,200,"success");
                    }else {
                        return new ResponseText(null,500,"候选人已在流程中");
                    }
                }else{
                    return new ResponseText(null,500,"候选人已在职");
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }

    }
    //修改应聘者信息
    @PutMapping("/interviewer")
    public ResponseText updateInterviewer(@RequestBody Interviewer interviewer){
        ResponseText responseText=null;
        try {
            interviewerService.updateInterviewer(interviewer);
            responseText=new ResponseText<PageBean<Interviewer>>(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //删除应聘者信息
    @DeleteMapping("/interviewer/{id}")
    public ResponseText deleteInterviewer(@PathVariable("id") int id){
        ResponseText responseText=null;
        try {
            interviewerService.deleteInterviewer(id);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

//    认证身份信息
    @RequestMapping("/validateInterviewer")
    public ResponseText validate(MultipartFile face,@RequestParam String name,@RequestParam String idCard) throws IOException {
        ResponseText responseText=null;
        try {
//            黑名单判断
            Employees employees = new Employees();
            employees.setState(5);
            PageBean<Employees> employeesPageBean = employeesService.selectEmpInfoCondition(employees, 1, 1000);
            for( Employees employees1:employeesPageBean.getBeanList()){
                if (employees1.getIdNum().equals(idCard)) {
                    return new ResponseText("黑名单",500,"fail");
                }
            }
            ValidateMsg validateMsg = interviewerService.validateInterviewer(face, name, idCard);
            responseText=new ResponseText(validateMsg,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
//    查询活动
    @PostMapping("/activity/{id}")
    public ResponseText<List<Activity>> getActivity(@PathVariable("id") int id){
        ResponseText<List<Activity>> responseText=null;
        try {
            List<Activity> activities = activityService.selectActivity(id);
            responseText=new ResponseText<List<Activity>>(activities,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    //导入面试人员信息
    @PostMapping("/importInterviewer")
    public ResponseText importInterviewerExcel(MultipartFile interviewerExcel){
        ResponseText responseText=null;
        try {
            interviewerService.importInterviewer(interviewerExcel);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //    导出面试人员信息
    @GetMapping("/exportInterviewer")
    public ResponseText exportInterviewerExcel(HttpServletResponse response) {
        ResponseText responseText=null;
        try {
           interviewerService.exportInterviewer(response);
            responseText=new ResponseText(null,200,"success");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
